package com.yuyou.activity.ts.service.service.impl;

import com.yuyou.activity.ts.service.entity.ActivityTsQuestionConfig;
import com.yuyou.activity.ts.service.entity.ActivityTsUser;
import com.yuyou.activity.ts.service.entity.ActivityTsUserDetail;
import com.yuyou.activity.ts.service.mapper.ActivityTsUserDetailMapper;
import com.yuyou.activity.ts.service.service.IActivityTsQuestionConfigService;
import com.yuyou.activity.ts.service.service.IActivityTsUserDetailService;
import com.yuyou.activity.ts.service.service.IActivityTsUserService;
import com.yuyou.activity.ts.service.vo.resp.RespExamResult;
import com.yuyou.activity.ts.service.vo.resp.RespRadar;
import com.yuyou.activity.util.enums.ActivityTsQuestionConfigEnums;
import com.yuyou.activity.util.enums.ActivityTsUserDetailEnums;
import com.yuyou.activity.util.enums.ActivityTsUserEnums;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by yujin.liu on 2018/12/19.
 */
@Service
public class ActivityTsUserDetailServiceImpl implements IActivityTsUserDetailService{

    @Autowired
    private ActivityTsUserDetailMapper activityTsUserDetailMapper;

    @Autowired
    private IActivityTsQuestionConfigService activityTsQuestionConfigService;

    @Autowired
    private IActivityTsUserService activityTsUserService;

    @Override
    public int updateByPrimaryKeySelective(ActivityTsUserDetail record) {
        return activityTsUserDetailMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public void submitAnswer(Long activityTsUserDetailId, String answer) {

        ActivityTsUserDetail activityTsUserDetailQuery = activityTsUserDetailMapper.selectByPrimaryKey(activityTsUserDetailId);
        // 提交答案，更新
        if(null != activityTsUserDetailQuery){
            ActivityTsQuestionConfig activityTsQuestionConfigQuery = activityTsQuestionConfigService.selectByPrimaryKey(activityTsUserDetailQuery.getActivityTsQuestionConfigId());
            ActivityTsUserDetail activityTsUserDetail = new ActivityTsUserDetail();
            activityTsUserDetail.setActivityTsUserDetailId(activityTsUserDetailId);
            activityTsUserDetail.setAnswer(answer);
            activityTsUserDetail.setIsRight(answer.equals(activityTsQuestionConfigQuery.getAnswerRight()) ? 1 : 0);
            activityTsUserDetail.setStatus(ActivityTsUserDetailEnums.stauts.answered.getValue());
            activityTsUserDetailMapper.updateByPrimaryKeySelective(activityTsUserDetail);
        }
    }

    @Override
    @Transactional(rollbackFor=Exception.class,timeout=60)
    public RespExamResult examResult(Long activityTsUserId) {
        RespExamResult respExamResult = new RespExamResult();
        ActivityTsUser activityTsUser = activityTsUserService.selectByPrimaryKey(activityTsUserId);
        if(null != activityTsUser){
            respExamResult.setActivityTsUserId(activityTsUser.getActivityTsUserId());
            // 查询测试结果
            List<RespRadar> respRadarList = activityTsUserDetailMapper.findExamResult(activityTsUserId);
            if(null == activityTsUser.getResult()){
                Double phyScore = new Double(0); // 物理分数
                Double geoScore = new Double(0); // 地理分数
                Double bioScore = new Double(0); // 生物分数
                Double chemScore = new Double(0); // 化学分数
                for (RespRadar radar: respRadarList) {
                    if(ActivityTsQuestionConfigEnums.subject.phy.getValue() == radar.getSubject()){
                        phyScore = radar.getScore();
                    }
                    if(ActivityTsQuestionConfigEnums.subject.geo.getValue() == radar.getSubject()){
                        geoScore = radar.getScore();
                    }
                    if(ActivityTsQuestionConfigEnums.subject.bio.getValue() == radar.getSubject()){
                        bioScore = radar.getScore();
                    }
                    if(ActivityTsQuestionConfigEnums.subject.chem.getValue() == radar.getSubject()){
                        chemScore = radar.getScore();
                    }
                }
                // 更新测试结果
                ActivityTsUser activityTsUserUpdate = new ActivityTsUser();
                activityTsUserUpdate.setActivityTsUserId(activityTsUser.getActivityTsUserId());
                if(phyScore >= 0.6){
                    activityTsUserUpdate.setResult(ActivityTsUserEnums.result.A.getValue()); // 物理高于 60%
                }else if(geoScore >= 0.4 || bioScore >= 0.4 || chemScore >= 0.4){
                    activityTsUserUpdate.setResult(ActivityTsUserEnums.result.B.getValue()); // 化、生、地 任意一科高于40%
                }else{
                    activityTsUserUpdate.setResult(ActivityTsUserEnums.result.C.getValue());
                }
                activityTsUserService.updateByPrimaryKeySelective(activityTsUserUpdate);
                activityTsUser.setResult(activityTsUserUpdate.getResult());
            }

            respExamResult.setResult(activityTsUser.getResult());
            respExamResult.setRadarList(respRadarList);
        }
        return respExamResult;
    }
}
